import 'dart:convert';
import './config.dart';
import 'package:lib_util/utils/utils.dart';

// @Entity()
class NonFungibleTokenMeta {
  // @Id()
  // int id;
  // int parentId;
  String? tokenId;
  String? tokenUri;
  String? metadata;
  String? balance;

  NonFungibleTokenMeta(
      {
      // this.id = 0,
      // required this.parentId,
      this.tokenId,
      this.tokenUri,
      this.metadata});

  TokenMetaData? decodeMetadata() {
    if (Utils.isEmpty(metadata)) return null;
    try {
      Map<String, dynamic> de = json.decode(metadata!);
      return TokenMetaData(
          name: de['name'],
          description: de['description'],
          image: de['image'],
          animationUrl: de['animation_url']);
    } catch (e) {
      Config.logger.e(e);
      return null;
    }
  }

  NonFungibleTokenMeta.fromJson(Map<String, dynamic> map) {
    tokenId = map['token_id'];
    tokenUri = map['token_uri'] ?? map['tokenUri'];
    metadata = map['normalized_metadata'] ?? map['metadata'];
    dynamic b = map['balance'];
    balance = b is int ? b.toString() : b;
  }
}

class TokenMetaData {
  String? name;
  String? description;
  String? image;
  String? animationUrl;

  TokenMetaData(
      {required this.name,
      required this.description,
      required this.image,
      required this.animationUrl});

  bool canShowAnimation() {
    return is3DModel() || isMusic() || isVideo();
  }

  bool is3DModel() {
    return Utils.isNotEmpty(animationUrl) &&
        (animationUrl!.endsWith('.glb') || animationUrl!.endsWith('.gltf'));
  }

  bool isMusic() {
    return Utils.isNotEmpty(animationUrl) &&
        (animationUrl!.endsWith('.mp3') || animationUrl!.endsWith('.acc'));
  }

  bool isVideo() {
    return Utils.isNotEmpty(animationUrl) &&
        (animationUrl!.endsWith('.mp4') || animationUrl!.endsWith('.avi'));
  }

  bool descriptionHtml() {
    return Utils.isNotEmpty(description) &&
        description!.startsWith('<p class=');
  }
}